#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
    目标：断言练习
    案例：
        输入：正确的用户名密码，验证码为空
        断言：提示信息是否为，验证码不能为空！
        要求：如果断言通过，截图保存。
"""
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By


class TestTpshopLogin(unittest.TestCase):

    def setUp(self):
        # 获取driver
        # 打开url
        # 最大化浏览器
        # 隐式等待
        self.driver = webdriver.Chrome()
        self.driver.get("https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx")
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    def tearDown(self):
        time.sleep(2)
        self.driver.quit()

    # 关闭浏览器
    def test_lobgin_code_null(self):
        drive = self.driver
        # 点击账号输入框，输入账号
        drive.find_element(By.CSS_SELECTOR, "#email").send_keys("123456@qq.com")
        # 点击密码框，输入密码
        drive.find_element(By.CSS_SELECTOR, "#pwd").send_keys("123456")
        # 点击验证码框，输入验证码
        drive.find_element(By.CSS_SELECTOR, "#code").send_keys("2345")
        # 点击登录
        drive.find_element(By.CSS_SELECTOR, "#denglu").click()
        alert = drive.switch_to.alert
        # 获取登录后的提示信息
        except_result = "提交失败，您输入的验证码有误！"
        try:
            self.assertEqual(alert.text, except_result)
        except AssertionError:
            # 截图
            print("截图")
            # drive.get_screenshot_as_file("./error.png")
            # 抛出异常
            raise
    # 失败截图
